package edu.pku.hawk.cloud;

import javax.jms.Message;
import javax.jms.MessageListener;

import javax.jms.ObjectMessage;

import edu.pku.hawk.controller.util.Constants;
import edu.pku.hawk.controller.util.MConsumerQueue;
import edu.pku.hawk.controller.util.MProducerQueue;
import edu.pku.hawk.controller.util.MQManager;
import edu.pku.hawk.controller.util.MQueue;
import edu.pku.hawk.controller.util.SerResource;
import edu.pku.hawk.controller.util.SerResourceList;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import edu.pku.hawk.cloud.cluster.ClusterController;
import edu.pku.hawk.cloud.util.CloudCfg;
import flex.messaging.io.amf.ASObject;
import flex.messaging.io.amf.translator.ASTranslator;

public class JMSListenner {

	static MQManager mgr = null;

	private static Map createResult;

	public static void main(String[] args) {

		try {
			mgr = new MQManager(Constants.MQServer_Name,
					Constants.MQServer_Password, Constants.MQServer_URL);
			MConsumerQueue queue = mgr
					.getConsumerQueue(Constants.Ctrl_Cloud_RscRequest_UI_QueueName);
			queue.setMessageListener(new MessageListener() {

				public void onMessage(Message message) {
					try {

						if (message instanceof ObjectMessage) {
							SerResourceList resourceList = (SerResourceList) MQueue
									.toOjbect(message);
							System.out.println("GET MSG!!!Create VM!!!");
							System.out.println("request:MEM:"
									+ resourceList.getTotalMem());

							//
							String tplUuid = CloudCfg.TEMPLATE_UUID;// CentOS_5.3_TPL
							
							System.out.println("Create VM: "+resourceList.isCreateVM());
							
							System.out.println("Create VM_2:" +createResult == null || resourceList.isCreateVM());
							
							
							if (createResult == null || resourceList.isCreateVM()) {
								createResult = ClusterController
										.createVmByName(tplUuid,
												(float) resourceList
														.getTotalCpu(),
												(float) resourceList
														.getTotalMem(),
												(float) resourceList
														.getTotalDisk(),
												(float) resourceList
														.getTotalNet());

							}
							
//							if (createResult == null) {
//								createResult = ClusterController
//										.createVmByName(tplUuid,
//												(float) resourceList
//														.getTotalCpu(),
//												(float) resourceList
//														.getTotalMem(),
//												(float) resourceList
//														.getTotalDisk(),
//												(float) resourceList
//														.getTotalNet());
//
//							}

							

							String vmIP = (String) createResult.get("VM_IP");
							String hostName = (String) createResult
									.get("HOST_NAME");
							String vmName = (String) createResult
									.get("VM_NAME");

							// String vmIP = "VM_IP";
							// String hostName = "HOST_NAME";
							// String vmName = "VM_NAME";

							resourceList.setEnable(true);
							resourceList.setXenName(hostName);
							resourceList.setVmName(vmName);
							resourceList.setVmIP(vmIP);

							resourceList.ResourceList = convertArrayCollection(resourceList.ResourceList);

							List<SerResource> resources = resourceList.ResourceList;
							for (SerResource serResource : resources) {
								// System.out.println(serResource.getClass());

								serResource.setServerURI(vmIP);
							}

							try {
								// if(vmIP != null){
								// report.setEnable(true);
								// }
								// report.setEnable(true);
								// report.setUri(vmIP);
								// report.setXenName(hostName);
								// report.setVmName(vmName);
								MProducerQueue queue = mgr
										.getProducerQueue(Constants.Ctrl_Cloud_RscResponse_UI_QueueName);
								queue.send(resourceList);
							} catch (Exception ex) {
								ex.printStackTrace();
							}
						} else {
							String re = MQueue.toString(message);
							String[] cmd = re.split(":");
							if (((String) cmd[0]).equalsIgnoreCase("START")) {
								System.out.println("start vm:" + cmd[1]);
								ClusterController.startVmByName(cmd[1]);
							} else {
								System.out.println("stop vm:" + cmd[1]);
								ClusterController.stopVmByName(cmd[1]);
							}

						}
					} catch (Exception ex) {
						ex.printStackTrace();
					}

				}
			});

			// mgr.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			if (mgr != null) {
				try {
					mgr.close();
				} catch (Exception eex) {
					ex.printStackTrace();
				}
			}
		}

	}

	public static List<SerResource> convertArrayCollection(List array) {
		ArrayList<SerResource> myObjectArray = new ArrayList();
		ASTranslator ast = new ASTranslator();
		SerResource myObject;
		ASObject aso;

		for (int i = 0; i < array.size(); i++) {
			Object res = array.get(i);
			if (res instanceof ASObject) {
				myObject = new SerResource();
				aso = new ASObject();

				aso = (ASObject) array.get(i);
				aso.setType("edu.pku.hawk.controller.util.SerResource");
				myObject = (SerResource) ast.convert(aso, SerResource.class);
			} else {
				myObject = (SerResource) res;
			}
			myObjectArray.add(myObject);
		}
		return myObjectArray;
	}
}
